package com.cskaoyan.realm;

import com.cskaoyan.bean.MarketAdmin;
import com.cskaoyan.bean.MarketAdminExample;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.MarketUserExample;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.service.admin.AdminAuthService;
import com.cskaoyan.token.MarketToken;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Bowen Deng
 * @since 2022/06/28 19:50
 */
@Component
public class MallRealm extends AuthorizingRealm {

    @Autowired
    MarketAdminMapper adminMapper;

    @Autowired
    MarketUserMapper userMapper;

    @Autowired
    AdminAuthService adminAuthService;

    /**
     * 这里执行认证，认证是授权的前提
     *
     * @param authenticationToken
     * @return org.apache.shiro.authz.AuthenticationInfo
     * @author Bowen Deng
     * @since 2022/06/28 10:35 下午
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        // 通常根据AuthenticationToken中传入的用户名信息查询出该用户在数据库中的信息
        String type = ((MarketToken) authenticationToken).getType();
        String username = (String) authenticationToken.getPrincipal();
        if ("admin".equals(type)) {
            MarketAdminExample example = new MarketAdminExample();
            example.createCriteria().andUsernameEqualTo(username);
            List<MarketAdmin> marketAdmins = adminMapper.selectByExample(example);
            if (marketAdmins.size() == 1) {
                //说明数据库中有一条对应的信息
                MarketAdmin marketAdmin = marketAdmins.get(0);
                // 构造认证信息时，可以放入你需要的用户信息，而你放入的用户信息，可以作为Principals
                // 放入这个信息，是为了取出这个信息
                // 第二个参数credentials，是真实（正确）的密码，会和AuthenticationToken中的password做比较
                return new SimpleAuthenticationInfo(marketAdmin, marketAdmin.getPassword(), getName());
            }
        } else if ("wx".equals(type)) {
            MarketUserExample marketUserExample = new MarketUserExample();
            marketUserExample.createCriteria().andUsernameEqualTo(username);
            List<MarketUser> marketUsers = userMapper.selectByExample(marketUserExample);
            if (marketUsers.size() == 1) {
                MarketUser marketUser = marketUsers.get(0);
                return new SimpleAuthenticationInfo(marketUser, marketUser.getPassword(), getName());
            }
            //查询user表中的信息
        }
        return null;
    }

    /**
     * 这里执行授权
     *
     * @param principalCollection
     * @return org.apache.shiro.authz.AuthorizationInfo
     * @author Bowen Deng
     * @since 2022/06/28 10:35 下午
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        MarketAdmin primaryPrincipal = (MarketAdmin) principalCollection.getPrimaryPrincipal();
        List<String> permissions = adminAuthService.getPermissionsByAdminId(primaryPrincipal.getRoleIds());
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addStringPermissions(permissions);
        return simpleAuthorizationInfo;
    }

}
